<?php namespace Admin\Controller;
use Hdphp\Controller\Controller;
//后台——货品控制器
class CargoController extends CommonController{
	private $cargo_model;
	//HdPHP自定义构造函数
	public function __init()
	{
		//调用CommonController里的构造函数
		parent::__init();	
	}
	
	//货品列表
	public function index(){
		//接收id
		$goods_id = Q('get.goods_id',0,'intval');
		
		//导航区域显示 商品名称
		$goodsModel = new \Common\Model\Goods;
		$goods_name = $goodsModel->where("goods_id={$goods_id}")->pluck('goods_name');
		View::with('goods_name',$goods_name);

		//实例化货品Cargo模型
		$cargoModel = new \Common\Model\Cargo;
		
		if(IS_POST){
		    //p(Q("post."));
			if(!$cargoModel->store()) View::error($cargoModel->getError());
			View::success('添加成功',U('index',array('goods_id'=>Q('get.goods_id',0,'intval'))));
		}

		//商品属性表与类型属性表 关联 取数据
		//实例化商品属性Goods_attr模型  因为需要检索出来的数据 是 可编辑的 规格 ，规格的类型是fattr_type=0
		$gattrModel = new \Common\Model\Goods_attr;
		$gattrData = $gattrModel->join('fattr','fattr_fa_id','=','fa_id')
								->where("goods_goods_id={$goods_id} and fattr_type=0")
								->field('ga_id,fattr_name,ga_value,fattr_type,form_fid')
								->get();				
		//p($gattrData);
		$arr = array();
		if(is_array($gattrData)){
			foreach($gattrData as $k=>$v){
				//将他们全部以fattr_name分开组，分别取出来  放在一个组里面.
				$arr[$v['fattr_name']][] = $v;
			}
		}
		//p($arr);
		View::with('arr',$arr);
		
		//把货品表的内容显示在页面里
		$cargoData = $cargoModel->where("goods_goods_id={$goods_id}")->get();
		
		foreach ($cargoData as $k => $v) {
			$cargoData[$k]['spec']=explode(',', $v['spec']);
		}
		$gattrModel = new \Common\Model\Goods_attr;
		
		foreach ($cargoData as $k => $v) {
				foreach ($v['spec'] as $ke=> $va) {
					foreach ($gattrModel->get() as $key => $value) {
						if($va==$value['ga_id']){
							$cargoData[$k]['spec'][$ke]=$value['ga_value'];
						}	
					}
				}
		}
		//p($cargoData);exit;
		View::with('cargoData',$cargoData);
		View::with('goods_id',$goods_id);
		View::make();
	}

	//编辑
	public function edit(){
		//接收id
		$goods_id = Q('get.goods_id',0,'intval');
		//接收id
		$cargo_id = Q('get.cargo_id',0,'intval');
		
		//实例化货品Cargo模型
		$cargoModel = new \Common\Model\Cargo;
		
		if(IS_POST){
		    //p(Q("post."));exit;
		    if(!$cargoModel->edit()) View::error($cargoModel->getError());
			View::success('修改成功',U('index',array('goods_id'=>$goods_id)));
		}

		//商品属性表与类型属性表 关联 取数据
		//实例化商品属性Goods_attr模型
		$gattrModel = new \Common\Model\Goods_attr;
		$gattrData = $gattrModel->join('fattr','fattr_fa_id','=','fa_id')
								->where("goods_goods_id={$goods_id} and fattr_type=0")
								->field('ga_id,fattr_name,ga_value,fattr_type,form_fid')
								->get();				
		//p($gattrData);
		$arr = array();
		if(is_array($gattrData)){
			foreach($gattrData as $k=>$v){
				//将他们全部以fattr_name分开组，分别取出来  放在一个组里面.
				$arr[$v['fattr_name']][] = $v;
			}
		}
		//p($arr);
		View::with('arr',$arr);
		
		//获得旧数据
		$oldData = $cargoModel->where("cargo_id={$cargo_id}")->find();
		$oldData['spec'] = explode(',', $oldData['spec']);
		//p($oldData);exit;
		
		View::with('oldData',$oldData);
		View::with('goods_id',$goods_id);
		View::with('cargo_id',$cargo_id);
		View::make();
	}

	//删除
	public function del(){
		//接收id
		$goods_id = Q('get.goods_id',0,'intval');
		$cargo_id = Q('get.cargo_id',0,'intval');
		
		//实例化货品Cargo模型
		$cargoModel = new \Common\Model\Cargo;
		$cargoModel->where("cargo_id={$cargo_id}")->delete();
		View::success('删除成功',U('index',array('goods_id'=>$goods_id)));
	}
	
	//异步
	public function check(){

		//实例化货品Cargo模型
		$cargoModel = new \Common\Model\Cargo;
		
		$data = $cargoModel->get();
		
		echo json_encode($data);
	}
	
	
	
	
	
	
	
	
	
	
	
	
}

 ?>